import baseCofig from '../config.js'
const http = {
	//post请求
	post(url,data,loading,getData,header){
		url =baseCofig.request_url+url;
		return http.request(url,data,"POST",header,loading,getData);
	},
	//get请求
	get(url,data,loading,getData,header){
		url = baseCofig.request_url+url;
		return http.request(url,data,"GET",header,loading,getData);
	},
	//同步post请求
	async asyncPost(url,data,getData,header){
		url = baseCofig.request_url+url;
		return await http.request(url,data,"POST",header,false,getData);
	},
	//同步get请求
	async asyncGet(url,data,getData,header){
		url =baseCofig.request_url+url;
		return await http.request(url,data,"GET",header,false,getData);
	},
	//总请求
	request(url,data,method,header,loading,getData){
		method = method||"GET";
		data = data||{};
		header = header||{};
		if(loading==undefined){
			loading = true;
		}
		if(getData==undefined){
			getData = true;
		}
		if(baseCofig.debug){
			console.log(data);
		}
		return new Promise((resolve, reject)=>{
			if(loading===true){
				uni.showLoading({
					title:"加载中"
				})
			}
			uni.request({
				url:url,
				header:header,
				method:method,
				data:data,
				success(res) {
					if(baseCofig.debug.debug){
						console.log(res);
					}
					if(res.data.code==1){
						if(getData){
							resolve(res.data.data)
						}else{
							resolve(res.data)
						}
					}else{
						// #ifdef H5
						uni.showToast({
							icon:"none",
							title:res.data.msg
						})
						// #endif
						// #ifdef MP
						uni.showModal({
							title:"提示",
							content:res.data.msg,
							showCancel:false,
						})
						// #endif
					}
				},
				fail(res) {
					if(baseCofig.debug.debug){
						console.log(res);
					}
					reject(res);
				},
				complete() {
					if(loading===true){
						uni.hideLoading();
					}
				}
			})
		})
	}
}
export default http